package com.yuda.operationcenter.controller;

import com.alibaba.fastjson.JSON;
import com.aliyun.oss.OSS;
import com.yuda.common.core.utils.file.OssFileUtils;
import com.yuda.common.core.utils.poi.ExcelUtil;
import com.yuda.common.core.web.controller.BaseController;
import com.yuda.common.core.web.domain.AjaxResult;
import com.yuda.common.core.web.page.TableDataInfo;
import com.yuda.common.log.annotation.Log;
import com.yuda.common.log.enums.BusinessType;
import com.yuda.common.security.annotation.PreAuthorize;
import com.yuda.operationcenter.domain.OperBanner;
import com.yuda.operationcenter.service.IOperBannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * banner配置Controller
 *
 * @author wyn
 * @date 2021-04-12
 */
@RestController
@RequestMapping("/banner")
public class OperBannerController extends BaseController {
    @Autowired
    private IOperBannerService operBannerService;

    @Autowired
    private OSS ossClient;

    @Autowired
    private RedisTemplate<String, String> redisTemplateDB1;


    //本地上传图片到OSS，返回图片的url
    @PostMapping("/upload")                       //businessType:文件传送的类型
    public String uploadBanner(MultipartFile multipartFile) {

        String fileName = OssFileUtils.generateFileName(multipartFile);

        String url = null;

        try {
            ossClient.putObject("operationcenter", fileName, new ByteArrayInputStream(multipartFile.getBytes()));
            url = "http://operationcenter.oss-cn-beijing.aliyuncs.com" + "/" + fileName;
            //System.out.println("---------->上传成功，oss地址：" + url);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return url;
    }


    /**
     * 新增banner配置
     */
    @PreAuthorize(hasPermi = "gwoper:banner:add")
    @Log(title = "banner配置", businessType = BusinessType.INSERT)
    @PostMapping("/addBanner")
    public AjaxResult add(@RequestBody OperBanner operBanner) {
        System.out.println(operBanner.toString());
        return toAjax(operBannerService.insertOperBanner(operBanner));

    }

    /**
     * 删除banner配置
     */
    @PreAuthorize(hasPermi = "gwoper:banner:remove")
    @Log(title = "banner配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/deleteBanner/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(operBannerService.deleteOperBannerByIds(ids));
    }

    /**
     * 查询banner配置列表
     */
    @PreAuthorize(hasPermi = "gwoper:banner:list")
    @GetMapping("/list")
    public TableDataInfo list(OperBanner operBanner) {
        startPage();
        List<OperBanner> list = operBannerService.selectOperBannerList(operBanner);
        return getDataTable(list);
    }


    /**
     * 修改banner配置
     */
    @PreAuthorize(hasPermi = "gwoper:banner:edit")
    @Log(title = "banner配置", businessType = BusinessType.UPDATE)
    @PutMapping()
    public AjaxResult edit(@RequestBody OperBanner operBanner) {
        redisTemplateDB1.delete("yd_ks:web_static:cloud:banner:list");
        return toAjax(operBannerService.updateOperBanner(operBanner));
    }

    /**
     * 批量上下架
     *
     * @param on_sell 0下架 1上架 2待上架
     */

    @PreAuthorize(hasPermi = "gwoper:banner:publishout")
    @Log(title = "批量更新banner配置", businessType = BusinessType.UPDATE)
    @PutMapping("/{ids}/{on_sell}")
    public AjaxResult edit(@PathVariable(value = "ids") Long[] ids, @PathVariable(value = "on_sell") int on_sell) {

        redisTemplateDB1.delete("yd_ks:web_static:cloud:banner:list");
        //检查能否上架
        List<Long> longs = new ArrayList<>();
        if (on_sell == 1) { //上架
            for (Long id : ids) {

                int i = operBannerService.checkIsCanUpdateStatus(id);

                if (i == 0) {
                    longs.add(id);
                } else {
                    return new AjaxResult(400, id + "号banner图信息不全，不可上架！");
                }
            }
            Long[] longs1 = new Long[longs.size()];
            return toAjax(operBannerService.batchUpdateSellStatus(longs.toArray(longs1), on_sell));
        }

        return toAjax(operBannerService.batchUpdateSellStatus(ids, on_sell));
    }

/*    @PreAuthorize(hasPermi = "operationcenter:banner:edit")
    @Log(title = "批量更新banner配置", businessType = BusinessType.UPDATE)
    @PutMapping("/{ids}/{on_sell}")
    public AjaxResult edit(@PathVariable(value = "ids") Long[] ids, @PathVariable(value = "on_sell") int on_sell)
    {
        //检查能否上架
        List<Long> longs = new ArrayList<>();
        List<Long> longs2 = new ArrayList<>();
        if (on_sell == 1) { //上架
            for (Long id : ids) {

                int i = operBannerService.checkIsCanUpdateStatus(id);
                if (i == 0)
                    longs.add(id);//可以上架id集合
                else
                    longs2.add(id);//不可上架id集合
            }
            if(longs2.size()==0){
            Long[] longs1 = new Long[longs.size()];
            return toAjax(operBannerService.batchUpdateSellStatus(longs.toArray(longs1), on_sell));
        }
        return toAjax(operBannerService.batchUpdateSellStatus(ids, on_sell));
    }*/

    /**
     * 获取banner配置详细信息       (筛选查询banner)
     */
    @PreAuthorize(hasPermi = "gwoper:banner:query")
    @GetMapping(value = "/getBannerMessage/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(operBannerService.selectOperBannerById(id));
    }

    /**
     * 导出banner配置列表
     */
    @PreAuthorize(hasPermi = "gwoper:banner:export")
    @Log(title = "banner配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    //@GetMapping("/export")
    public void export(HttpServletResponse response, OperBanner operBanner) throws IOException {
        List<OperBanner> list = operBannerService.selectOperBannerList(operBanner);
        ExcelUtil<OperBanner> util = new ExcelUtil<OperBanner>(OperBanner.class);
        util.exportExcel(response, list, "banner");
    }


    @GetMapping("/test")
    public String test() {
        return "李慧。avi";
    }


    @GetMapping("/getBanner/{id}")
    public String test02(@PathVariable("id") int id) {

        List<OperBanner> list = operBannerService.selectById(id);
        System.out.println(list);
        return JSON.toJSONString(list);

    }

    @GetMapping("/getBanner2/{ids}")
    public String test03(@PathVariable("ids") long[] ids) {

        List<OperBanner> list = operBannerService.selectByIds(ids);
        System.out.println(list);
        return JSON.toJSONString(list);

    }
}
